package com.smy.service.impl;
import com.smy.common.BaseDao;
import com.smy.common.MyServiceImpl;
import com.smy.dao.UserDao;
import com.smy.dao.impl.EmployeeDaoImpl;
import com.smy.dao.impl.EmployeeDetailDaoImpl;
import com.smy.dao.impl.ExamineDaoImpl;
import com.smy.dao.impl.UserDaoImpl;
import com.smy.dto.EmployeeDto;
import com.smy.pojo.Employee;
import com.smy.pojo.EmployeeDetail;
import com.smy.pojo.User;
import com.smy.service.EmployeeDetailService;
import com.smy.service.EmployeeService;
import com.smy.service.UserService;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created on 2023/10/12.
 *
 * @author smy
 */
public class EmployeeServiceImpl extends MyServiceImpl<EmployeeDaoImpl, Employee> implements EmployeeService {

    private EmployeeDetailService employeeDetailService = new EmployeeDetailServiceImpl(EmployeeDetailDaoImpl.getInstance());
    private UserService userService = new UserServiceImpl(UserDaoImpl.getInstance());

    public EmployeeServiceImpl(EmployeeDaoImpl employeeDao) {
        super(employeeDao);
    }

    public void saveEmployee(EmployeeDto employeeDto){
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            assert connection != null;
            connection.setAutoCommit(false);
            //添加员工
            this.save(employeeDto,connection);
            //添加员工详情
            EmployeeDetail employeeDetail = employeeDto.getEmployeeDetail();
            //绑定员工id
            employeeDetail.setEmployeeId(employeeDto.getId());
            employeeDetailService.save(employeeDetail, connection);
            //添加账户
            User user = employeeDto.getUser();
            //绑定员工id
            user.setEmployeeId(employeeDto.getId());
            userService.save(user, connection);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            try {
                assert connection != null;
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
